//Main
app.controller('selectAreaAllCtr',["$scope","$rootScope","selectAreaAllService","selectDatas", function($scope,$rootScope,selectAreaAllService,selectDatas) {
    var areaDataVersion = {  //省市县地址版本号
        newVersion : 4,
        oldVersion : 3
    };

    $scope.areaData = [];
    $scope.areaSelectData = {
        areaSelectIndex : 0,
        countySelectIndex : -1,
        areaData : []
    };

    function resSelect(){
        function selectDataFn(data,name,parData){
            for(var i = 0;i < data.length;i++){
                if(data[i].areaName == name){
                    data[i].isSelect = true;
                    selectToggle(data[i].childList,true);
                }
                if(parData && !parData.isSelect){
                    parSelectToggle(data[i],parData,0,true);   //回填选中数量
                }
                if(data[i].provinceAreaDTOList){
                    arguments.callee(data[i].provinceAreaDTOList,name,data[i]);
                }
                if(data[i].childList && data[i].childList.length){
                    arguments.callee(data[i].childList,name,data[i]);
                }
            }
        }

        ;(function(selectData){
            for(var i = 0;i < selectData.length;i++){
                if(selectData[i].childList && selectData[i].childList.length){
                    arguments.callee(selectData[i].childList);
                }else{
                    selectDataFn($scope.areaData,selectData[i].name)
                }
            }
        })(selectDatas.selectData || []);
    }

    function getSelectData(data,parAreaName,tarData,selectName){
        for(var i = 0;i <= data.length-1;i++){
            var item = {
                name : data[i].areaName,
                parName : parAreaName,
                childList : []
            };
            if(data[i].provinceAreaDTOList){
                arguments.callee(data[i].provinceAreaDTOList,data[i].areaName,tarData,selectName);
            }else{
                if(data[i].isSelect){
                    // console.log(data[i])
                    selectName.push(data[i].areaName);
                }
                if(data[i].isSelect || data[i].isChildSelect) {
                    tarData.push(item);
                }
                if(data[i].childList && data[i].isChildSelect && !data[i].isSelect){
                    arguments.callee(data[i].childList,data[i].areaName,item.childList,selectName);
                }
            }
        }
        return tarData;
    }

    function selectNum(data){
        var selestNum = 0;
        angular.forEach(data,function(item){
            if(item.isSelect){
                selestNum++;
            }
        });
        return selestNum;
    }

    function childSelectToggle(parData,selectTrue){
        if(selectTrue){
            parData.isChildSelect = true;
        }else{
            parData.isChildSelect = false;
        }
    }

    function selectToggle(data,isSelect){
        angular.forEach(data,function(item){
            if(isSelect){
                item.isSelect = true;
                item.selectNum = 0;   //全部选中子区或者县选择的数量清空
            }else{
                item.isSelect = false;
            }
            if(item.childList){
                selectToggle(item.childList,isSelect);
            }
        });
    }

    function parSelectToggle(data,parData,childSelectNum,notSelectToggle){
        var parListData = parData.provinceAreaDTOList || parData.childList;
        var selectNumTotal = selectNum(parListData);
        if(data.isSelect){
            if(selectNumTotal == parListData.length){
                parData.isSelect = true;
                data.selectNum = 0;   //全部选中子区或者县选择的数量清空
            }
        }else{
            parData.isSelect = false;
            data.selectNum = 0;  //全部选中子区或者县选择的数量清空
        }
        if(parListData.length == 1){
            var childSelectNum = selectNum(parListData[0].childList);
            childSelectToggle(parData,childSelectNum)
        }else{
            childSelectToggle(parData,selectNumTotal || childSelectNum);
        }

        if(!notSelectToggle){
            selectToggle(data.childList,data.isSelect);
        }
        parData.selectNum = selectNumTotal;     //吧选中的值赋给父Object里面的一个字段

        return selectNumTotal;
    }

    function submitNext(){
        $scope.closeThisDialog();  //dialog 关闭
    }

    function getProvinceItemScope(parentId){
        return angular.element(document.getElementById('provinceData' + parentId)).scope().provinceItem;
    }


    $scope.provinceClick = function(data,index,evt){  //省点击显示市
        $scope.areaSelectData.areaSelectIndex = index;
        $scope.areaSelectData.areaData = [];
        angular.forEach(data.childList,function(item){
            $scope.areaSelectData.areaData.push(item);
        });
    };

    $scope.provinceSelectAll = function (data,parData) {   //省
        parSelectToggle(data,parData);
        childSelectToggle(data,data.isSelect);
    };

    $scope.areaSelectAll = function(data,parData){  //市点击
        var provinceData = getProvinceItemScope(data.parentId); //获取省scope数据
        parSelectToggle(data,parData);  //判断大陆是否选中
        parSelectToggle(data,provinceData); //判断省是否选中,并且返回选中的个数
    };

    $scope.selectAll = function(data,isSelect){  //华东大区选择
        selectToggle(data,isSelect);
    };

    $scope.countySelect = function(itemData,areaItemData,provinceItemData){  //区选择
        var provinceData = getProvinceItemScope(areaItemData.parentId); //获取省scope数据
        parSelectToggle(itemData,areaItemData); //判断市是否全选,并且返回选中的个数
        parSelectToggle(itemData,provinceData,areaItemData.selectNum);  //判断省是否选中

        parSelectToggle(itemData,provinceItemData,provinceData.selectNum);  //判断大陆是否选中
    };

    $scope.countyToggle = function(index){
        if($scope.areaSelectData.countySelectIndex == index){
            $scope.areaSelectData.countySelectIndex = -1;
            return;
        }
        $scope.areaSelectData.countySelectIndex = index;
    };

    $scope.submit = function(){
        var selectData = [];
        var selectName = [];
        getSelectData($scope.areaData,'',selectData,selectName);
        if(selectDatas.selectCallback){
            selectDatas.selectCallback(selectName,selectData,submitNext);
            return;
        }
        submitNext();
    };


    ;(function(){
        var  areaName = ('areaData' + areaDataVersion.newVersion);
        var  areaOldName = ('areaData' + areaDataVersion.oldVersion);

        if(localStorage.getItem(areaOldName)){
            localStorage.removeItem(areaOldName);
        }

        if(localStorage.getItem(areaName)){
            $scope.areaData = JSON.parse(localStorage.getItem(areaName));
            resSelect(); // 修改进来的话数据回填
            return
        }

        selectAreaAllService.area(function(data){
            $scope.areaData = data.data;
            localStorage.setItem(areaName,JSON.stringify($scope.areaData));
            resSelect(); // 修改进来的话数据回填
        });
    })();
}]);
